# Main routine
def poda():
    import sys
    import time
    import readinp
    import CheckParam
    import lattice
    import countdefect
    # readinp
    # set_param
    # set_struct
    # set_neighbor
    # count from the files
    if __name__=="__main__":

        try:
            ifn=sys.argv[1]
        except:
            print("Specify input file name.")
            sys.exit(2)

        try:
            ofn=sys.argv[2]
        except:
            print("Specify input file name.")
            sys.exit(2)

    else:

        ifn=input(" - Specify input file name\n")
        ofn=input(" - Specify output file name\n")

    t0=time.time()
    lfn="log%.0f" % t0

    ifp=open(ifn.strip(),'r')
    lfp=open(lfn.strip(),'w')
    ofp=open(ofn.strip(),'w')

    readinp.readinp(ifp)
    readinp.readsource()
    readinp.sourceparam(0)
    # Record the case information
    str1=" "+time.asctime(time.gmtime(t0))+"\n"
    str2=" Input file name: %s\n Output file name: %s\n Log file name: %s\n" % (ifn,ofn,lfn)
    sys.stdout.write(str1)
    sys.stdout.write(str2)
    ofp.write(str1)
    ofp.write(str2)
    lfp.write(str1)
    lfp.write(str2)
    CheckParam.check_input(lfp)

    lattice.make_basis(lfp)
    CheckParam.check_cell_param(lfp)
    CheckParam.check_basis(lfp)

    countdefect.countbysource(ofp,lfp)
    #CheckParam.check_basis(ofp)

    t1=time.time()
    str_dt=" Total running time = %.1f (sec)\n" % (t1-t0)
    sys.stdout.write(str_dt)
    lfp.write(str_dt)
    ofp.write(str_dt)

    ifp.close()
    lfp.close()
    ofp.close()
            
# Execute main
poda()
